﻿using BZ.Core;
using DllSampleAll;
using LJV7_DllSampleAll;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace NDK.AcqDevice.Keyence
{
    public class Laser_LKIF2 : IDevice,IPointLaser
    {
        string _IP = "192.168.10.10";
        private const int MaxRequestDataLength = 512000;

        public event PropertyChangedEventHandler PropertyChanged;
        private string _status = string.Empty;
        public string Status
        {
            get
            {
                return _status;
            }
            private set
            {
                _status = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(value.ToString()));
            }
        }
        public string IP
        {
            get
            {
                return _IP;
            }

            set
            {
                _IP = value;
            }
        }
        [DllImport("wsock32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
        extern public static int inet_addr([MarshalAs(UnmanagedType.VBByRefStr)] ref string cp);
        public Laser_LKIF2(string IP)
        {
            _IP = IP;
        }

        public bool Open()
        {
            try
            {
                Status = $"PointLaser （通讯口{IP}) Connecting...";
                LKIF2.LKIF_OPENPARAM_ETHERNET ethernetSetting = new LKIF2.LKIF_OPENPARAM_ETHERNET();
                ethernetSetting.IPAddress.S_addr = inet_addr(ref _IP);
                LKIF2.RC rc = LKIF2.LKIF2_OpenDeviceETHER(ref ethernetSetting);
                if (rc == LKIF2.RC.RC_OK)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                Status = $"PointLaser （通讯口{IP}) LKIF2_OpenDeviceETHER  Exception Message:{ex.Message} Exception StackTrace {ex.StackTrace}";
                throw new LaserException($"LKIF2_OpenDeviceETHER Exception！Stacktrace：{ex.StackTrace}");

            }
        }
        public bool Close()
        {
            try
            {
                LKIF2.RC rc = LKIF2.LKIF2_CloseDevice();
                if (rc == LKIF2.RC.RC_OK)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                Status = $"PointLaser （通讯口{IP}) LKIF2_CloseDevice  Exception Message:{ex.Message} Exception StackTrace {ex.StackTrace}";
                throw new LaserException($"LKIF2_CloseDevice Exception！Stacktrace：{ex.StackTrace}");
            }


        }
        /// <summary>
        /// 读取指定通道的值，超时时间未使用
        /// </summary>
        /// <param name="index"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public string Read(int index,int timeout)
        {
            try
            {
                LKIF2.LKIF_FLOATVALUE_OUT calcData = new LKIF2.LKIF_FLOATVALUE_OUT();
                LKIF2.RC rc = LKIF2.LKIF2_GetCalcDataSingle(index, ref calcData);
                string ret = FloatResultValueOutToText(calcData);
                Status = $"PointLaser （通讯口{IP})， Chanel {index}，  Value：{ret}";

                if (rc == LKIF2.RC.RC_OK)
                {
                    return ret;
                }
                else
                {
                    return ret;
                }
            }
            catch (Exception ex)
            {
                Status = $"PointLaser （通讯口{IP}) LKIF2_GetCalcDataSingle  Exception Message:{ex.Message} Exception StackTrace {ex.StackTrace}";
                throw new LaserException($"LKIF2_GetCalcDataSingle Exception！Stacktrace：{ex.StackTrace}");
            }

        }

        private string FloatResultValueOutToText(LKIF2.LKIF_FLOATVALUE_OUT FloatValue)
        {

            if (FloatValue.FloatResult == LKIF2.LKIF_FLOATRESULT.LKIF_FLOATRESULT_VALID)
            {
                return FloatValue.value.ToString();
            }
            else if (FloatValue.FloatResult == LKIF2.LKIF_FLOATRESULT.LKIF_FLOATRESULT_RANGEOVER_P)
            {
                return "+FFFFFFF";
            }
            else if (FloatValue.FloatResult == LKIF2.LKIF_FLOATRESULT.LKIF_FLOATRESULT_RANGEOVER_N)
            {
                return "-FFFFFFF";
            }
            else if (FloatValue.FloatResult == LKIF2.LKIF_FLOATRESULT.LKIF_FLOATRESULT_WAITING)
            {
                return "--------";
            }
            else if (FloatValue.FloatResult == LKIF2.LKIF_FLOATRESULT.LKIF_FLOATRESULT_ALARM)
            {
                return "Alarm";
            }
            else
            {
                return "Invalid";
            }
        }
    }
}
